package com.ybzl.mini.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ybzl.common.core.controller.BaseController;
import com.ybzl.common.core.controller.IdGenerator;
import com.ybzl.common.core.domain.ResultVo;
import com.ybzl.common.core.page.PageData;
import com.ybzl.common.utils.DateUtils;
import com.ybzl.common.utils.SecurityUtils;
import com.ybzl.mini.entity.MiniGood;
import com.ybzl.mini.entity.MiniWenzhang;
import com.ybzl.mini.service.IMiniWenzhangService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/*
 * Desc：小程序文章管理列表
 * @Author jiaxin
 * @Company asiainfo.com
 * @Since  2024-06-29 10:44:56.540
 *
 */

@Slf4j
@RestController
@RequestMapping("/miniWenzhang")
public class MiniWenzhangController extends BaseController {

    @Autowired
    private IMiniWenzhangService iMiniWenzhangService;

    /**
     * 新增 MiniWenzhang
     *
     * @param miniWenzhang miniWenzhang
     * @return data data
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody MiniWenzhang miniWenzhang) {
        if (null == miniWenzhang.getId()) {
            miniWenzhang.setId(IdGenerator.orderId(MiniWenzhangController.class));
            miniWenzhang.setCreateBy(SecurityUtils.getUsername());
            miniWenzhang.setStatus("0");
            iMiniWenzhangService.save(miniWenzhang);
        } else {
            miniWenzhang.setUpdateTime(DateUtils.getTime());
            miniWenzhang.setUpdateBy(SecurityUtils.getUsername());
            iMiniWenzhangService.updateById(miniWenzhang);
        }
        return ResultVo.success();

    }

    /**
     * 更新 MiniWenzhang
     *
     * @param miniWenzhang miniWenzhang
     * @retur data data
     */
    @PutMapping("/update")
    public ResultVo update(@RequestBody MiniWenzhang miniWenzhang) {
        miniWenzhang.setUpdateBy(SecurityUtils.getUsername());
        iMiniWenzhangService.updateById(miniWenzhang);
        return ResultVo.success();
    }

    /**
     * 查询 MiniWenzhang 列表，模糊查询
     *
     * @param miniWenzhang miniWenzhang
     * @return data data
     */
    @PostMapping("/list")
    public PageData list(@RequestBody MiniWenzhang miniWenzhang) {
        startPage();
        LambdaQueryWrapper<MiniWenzhang> query = new LambdaQueryWrapper<>();
        query.orderByAsc(MiniWenzhang::getSort);
        return getDataTable(iMiniWenzhangService.list(query));
    }

    /**
     * 根据主键查询
     *
     * @param keyId keyId
     * @return info info
     */
    @GetMapping("/detail/{keyId}")
    public ResultVo detail(@PathVariable("keyId") String keyId) {
        return ResultVo.success(iMiniWenzhangService.getById(keyId));
    }


    @DeleteMapping("/delete/{keyId}")
    public ResultVo delete(@PathVariable("keyId") String keyIds) {

        if (keyIds.indexOf(",") > 0) {
            String[] keyIdArray = keyIds.split(",");
            for (String key : keyIdArray) {
                iMiniWenzhangService.removeById(key);
            }
        }
        iMiniWenzhangService.removeById(keyIds);
        return ResultVo.success();
    }
}
